import java.util.Arrays;

public class SeqList
{
    public Object[] arr;//动态数组
    public int arrSize;//有效元素个数
    public static final int capacity = 4;//顺序表初始容量

    //初始化顺序表
    public SeqList()
    {
        this.arr = new Object[capacity];
        this.arrSize = 0;
    }


    public boolean isEmpty()//判断是否空
    {
        return this.arrSize == 0;
    }

    public void CheckCapacity()//检查是否需要扩容
    {
        if (this.arrSize == capacity)
        {
            this.arr = Arrays.copyOf(this.arr, arrSize * 2);
        }
    }

    public int size()//返回元素个数（长度）
    {
        return this.arr.length;
    }

    public String toString()//所有元素的描述字符串
    {
        String print = "";
        if (this.arrSize == 0)
        {
            return "顺序表为空";
        }
        for (int i = 0; i < this.arrSize; i++)
        {
            print += arr[i] + " ";
        }
        return print;
    }

    public int search(Object key)//查找并返回与key相等元素
    {
        if (isEmpty())
        {
            return -1;
        }
        for (int i = 0; i < this.arrSize; i++)
        {
            if (this.arr[i] == key)
            {
                return i;
            }
            else
            {
                i++;
            }
        }
        return -1;
    }

    public void remove(Object key)//查找并删除与key相等元素
    {
        if (isEmpty())
        {
            System.out.println("顺序表为空，无法删除");
        }
        for (int i = 0; i < this.arrSize; i++)
        {
            if (this.arr[i] == key)
            {
                int start = i + 1;
                //挪动数据
                while (start < this.arrSize)
                {
                    this.arr[start - 1] = this.arr[start];
                    start++;
                }
                this.arrSize--;//删除后顺序表大小减一
            }
        }
    }

    public void clear()//删除所有元素
    {
        this.arrSize = 0;
    }

    public boolean equals(Object obj)//比较this与obj是否相等
    {
        for (int i = 0; i < arrSize; i++)
        {
            if (this.arr[i] == obj)
            {
                return true;
            }
        }
        return false;
    }

    public int get(int i)//返回第i个元素
    {
        return i;
    }

    public void set(int i, int x)//设置第i个元素为x
    {
        this.arr[i] = x;
    }

    public void insert(int i, int x)//插入x作为第i个元素
    {
        CheckCapacity();
        int end = this.arrSize - 1;
        //挪动数据
        while (end >= i)
        {
            this.arr[end + 1] = this.arr[end];
            end--;
        }
        this.arr[i] = x;
        this.arrSize++;
    }

    public void insert(int x)//在最后插入x元素
    {
        CheckCapacity();
        if (isEmpty())
        {
            this.arr[0] = x;
        }
        else
        {
            this.arr[this.arrSize] = x;
        }
        this.arrSize++;
    }

    public void remove(int i)//删除第i个元素
    {
        if (isEmpty())
        {
            System.out.println("顺序表为空，无法删除");
        }
        int start = i + 1;
        //挪动数据
        while (start < this.arrSize)
        {
            this.arr[start - 1] = this.arr[start];
            start++;
        }
        this.arrSize--;//删除后顺序表大小减一
    }
}
